<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

?>

<div class="fieldset-wrapper" id="add-products-to-website-wrapper">
    <fieldset class="fieldset" id="grop_fields">
        <legend class="legend">
            <span><?php /* @escapeNotVerified */ echo __('Add Product To Websites') ?></span>
        </legend>
        <br>
        <div class="store-scope">
            <div class="store-tree" id="add-products-to-website-content">
                <?php foreach ($block->getWebsiteCollection() as $_website): ?>
                    <div class="website-name">
                        <input name="add_website_ids[]" value="<?php /* @escapeNotVerified */ echo $_website->getId() ?>" <?php if ($block->getWebsitesReadonly()): ?>disabled="disabled"<?php endif;?> class="checkbox website-checkbox" id="add_product_website_<?php /* @escapeNotVerified */ echo $_website->getId() ?>" type="checkbox" />
                        <label for="add_product_website_<?php /* @escapeNotVerified */ echo $_website->getId() ?>"><?php echo $block->escapeHtml($_website->getName()) ?></label>
                    </div>
                    <dl class="webiste-groups" id="add_product_website_<?php /* @escapeNotVerified */ echo $_website->getId() ?>_data">
                        <?php foreach ($block->getGroupCollection($_website) as $_group): ?>
                            <dt><?php echo $block->escapeHtml($_group->getName()) ?></dt>
                            <dd class="group-stores">
                                <ul>
                                <?php foreach ($block->getStoreCollection($_group) as $_store): ?>
                                    <li>
                                        <?php echo $block->escapeHtml($_store->getName()) ?>
                                    </li>
                                <?php endforeach; ?>
                                </ul>
                            </dd>
                        <?php endforeach; ?>
                    </dl>
                <?php endforeach; ?>
            </div>
        </div>
    </fieldset>
</div>

<div class="fieldset-wrapper" id="remove-products-to-website-wrapper">
    <fieldset class="fieldset" id="grop_fields">
        <legend class="legend">
            <span><?php /* @escapeNotVerified */ echo __('Remove Product From Websites') ?></span>
        </legend>
        <br>
        <div class="messages">
            <div class="message message-notice">
                <div><?php /* @escapeNotVerified */ echo __('To hide an item in catalog or search results, set the status to "Disabled".') ?></div>
            </div>
        </div>
        <div class="store-scope">
            <div class="store-tree" id="remove-products-to-website-content">
                <?php foreach ($block->getWebsiteCollection() as $_website): ?>
                    <div class="website-name">
                        <input name="remove_website_ids[]" value="<?php /* @escapeNotVerified */ echo $_website->getId() ?>" <?php if ($block->getWebsitesReadonly()): ?>disabled="disabled"<?php endif;?> class="checkbox website-checkbox" id="remove_product_website_<?php /* @escapeNotVerified */ echo $_website->getId() ?>" type="checkbox" />
                        <label for="remove_product_website_<?php /* @escapeNotVerified */ echo $_website->getId() ?>"><?php echo $block->escapeHtml($_website->getName()) ?></label>
                    </div>
                    <dl class="webiste-groups" id="remove_product_website_<?php /* @escapeNotVerified */ echo $_website->getId() ?>_data">
                        <?php foreach ($block->getGroupCollection($_website) as $_group): ?>
                            <dt><?php echo $block->escapeHtml($_group->getName()) ?></dt>
                            <dd class="group-stores">
                                <ul>
                                <?php foreach ($block->getStoreCollection($_group) as $_store): ?>
                                    <li>
                                        <?php echo $block->escapeHtml($_store->getName()) ?>
                                    </li>
                                <?php endforeach; ?>
                                </ul>
                            </dd>
                        <?php endforeach; ?>
                    </dl>
                <?php endforeach; ?>
            </div>
        </div>
    </fieldset>
</div>

<script>
require([
    'prototype'
], function () {
    var productWebsiteCheckboxes = $$('.website-checkbox');

    for(var i=0;i<productWebsiteCheckboxes.length;i++){
        Event.observe(productWebsiteCheckboxes[i], 'click', toggleStoreFromChoosers);
    }

    function toggleStoreFromChoosers(event){
        var element = Event.element(event);
        var selects = $(element.id+'_data').getElementsBySelector('select');
        var selectBlocks = $(element.id+'_data').getElementsByClassName('website-'+element.value+'-select');
        for (var i=0; i<selects.length; i++) {
            selects[i].disabled=!element.checked;
        }
        for (var i=0; i<selectBlocks.length; i++) {
            if (element.checked) {
                selectBlocks[i].show();
            }
            else {
                selectBlocks[i].hide();
            }
        }
    }
});
</script>
